import {Component, OnInit, Output, EventEmitter, Input, ViewEncapsulation} from '@angular/core';
import {ToolsService} from "../../service/tools/tools.service";

@Component({
  selector: 'seed-sponsor-filterPage',
  templateUrl: './sponsor-filter.component.html',
  styleUrls: ['./sponsor-filter.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class SponsorFilterComponent implements OnInit {
  public filterObj = {
    areaList: [],
    endMonth: '',
    moneyValue: '',
    page: 1,
    rows: 15,
    proList: [],
    startMonth: ''
  };
  public filterParamsReset: any;
  public initialTime: any;
  public initialYear: any;
  public initialMonth: any;

  public startTime: any;
  public endYear: any;
  public endMonth: any;
  public startYear: any;
  public startMonth: any;

  public competitionShow = true;
  public areaShow = true;
  @Output() closePages = new EventEmitter();
  @Output() submitDate = new EventEmitter();
  @Input() filterData: any;
  @Input() filterParam: any;
  @Input() proListName: any;
  @Input() areaName: any;

  constructor(public toolsService: ToolsService) {

  }

  ngOnInit() {
    this.filterParamsReset = {...this.filterObj};
    this.filterObj = {...this.filterParam};
    this.filterObj.page = 1;
    this.DealStartTime();
  }

  DealStartTime() {
    const getInitial = new Date(this.filterData.currentTime);
    this.initialYear = getInitial.getFullYear();
    this.initialMonth = getInitial.getMonth() + 1;
    if (this.initialMonth < 10) {
      this.initialMonth = '0' + this.initialMonth;
    }
    this.initialTime = this.initialYear + '-' + this.initialMonth;
    if (this.filterObj.startMonth === '') {
      this.filterObj.startMonth = this.initialTime;
    }

    this.startYear = this.initialYear;
    this.startMonth = this.initialMonth;
  }

  CloseFilterPage() {
    this.closePages.emit();
  }

  ToggleCompetition(name) {
    if (name === 'sportType') {
      this.competitionShow = !this.competitionShow;
    } else {
      this.areaShow = !this.areaShow;
    }
  }

  SportType(sportName, sportId) {
    if (sportName !== '全部') {
      if (this.filterObj.proList.indexOf(sportId) === -1) {
        this.filterObj.proList = [...this.filterObj.proList, sportId];
        this.proListName = [...this.proListName, sportName];
      } else {
        this.proListName.splice(this.proListName.indexOf(sportName), 1);
        this.proListName = [...this.proListName];
        this.filterObj.proList.splice(this.filterObj.proList.indexOf(sportId), 1);
      }
    } else {
      this.filterObj.proList = [];
      this.proListName = [];
    }
  }

  AreaChose(areaName, areaId) {
    if (areaName !== '全部') {
      if (this.filterObj.areaList.indexOf(areaId) === -1) {
        this.filterObj.areaList = [...this.filterObj.areaList, areaId];
        this.areaName = [...this.areaName, areaName];
      } else {
        this.filterObj.areaList.splice(this.filterObj.areaList.indexOf(areaId), 1);
        this.areaName.splice(this.areaName.indexOf(areaName), 1);
        this.areaName = [...this.areaName];
      }
    } else {
      this.filterObj.areaList = [];
      this.areaName = [];
    }
  }

  MoneyChose(moneyValue) {
    this.filterObj.moneyValue = moneyValue;
  }

  StartChange() {
    if (this.filterObj.startMonth === '' || parseInt(this.filterObj.startMonth.slice(0, 4), 10) < parseInt(this.initialYear, 10)) {
      this.filterObj.startMonth = this.initialTime;
    } else if (this.filterObj.startMonth.slice(0, 4) === this.initialYear) {
      if (this.filterObj.startMonth.slice(5) < this.initialMonth) {
        this.filterObj.startMonth = this.initialTime;
      } else {
        this.startYear = this.filterObj.startMonth.slice(0, 4);
        this.startMonth = this.filterObj.startMonth.slice(5);
      }
    } else {
      this.startYear = this.filterObj.startMonth.slice(0, 4);
      this.startMonth = this.filterObj.startMonth.slice(5);
    }
  }

  StartBlur() {
    if (this.filterObj.startMonth === '' || this.startYear < this.initialYear) {
      this.filterObj.startMonth = this.initialTime;
    }
  }

  EndTimeBlur() {
    /*if (this.filterObj.endMonth !== '' && this.endYear < this.startYear) {
      this.toolsService.showToast('结束日期必须大于或等于开始日期', 3000);
    } else if (this.endYear === this.startYear && this.endMonth < this.startMonth) {
      this.toolsService.showToast('结束日期必须大于或等于开始日期', 3000);
    }*/
    if ((this.filterObj.endMonth !== '' && this.endYear < this.startYear)
      || ( this.endYear === this.startYear && this.endMonth < this.startMonth)) {
      this.toolsService.showToast('结束日期必须大于或等于开始日期',3000);
    }
  }

  Endchange() {
    this.endYear = this.filterObj.endMonth.slice(0, 4);
    this.endMonth = this.filterObj.endMonth.slice(5);
    if (this.filterObj.endMonth !== '' && this.filterObj.endMonth.slice(0, 4) < this.startYear) {
      this.filterObj.endMonth = this.filterObj.startMonth;
    } else if (this.filterObj.endMonth.slice(0, 4) === this.startYear) {
      if (this.filterObj.endMonth.slice(5) < this.startMonth) {
        this.filterObj.endMonth = this.filterObj.startMonth;
      }
    } else if (this.filterObj.endMonth === '') {
      this.filterObj.endMonth = '';
    }
  }

  Reset() {
    this.filterObj = {...this.filterParamsReset};
    this.filterObj.startMonth = this.initialTime;
    this.proListName = [];
    this.areaName = [];
  }

  Submit() {
    this.closePages.emit();
    this.submitDate.emit();
  }

}
